import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 链表
 * User: mac
 * Date: 2022-08-31
 * Time: 10:09
 */

//ListNode代表一个节点
class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class MyLinkedList {
    public ListNode head;//链表的头引用

    public void createList() {
        ListNode listNode1 = new ListNode((12));
        ListNode listNode2 = new ListNode((23));
        ListNode listNode3 = new ListNode((34));
        ListNode listNode4 = new ListNode((45));
        ListNode listNode5 = new ListNode((56));

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    //打印链表
    public void display() {
        //this.head.next != null 会丢失一个数据
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定头结点开始打印
     *
     * @param key
     * @return
     */
    //指定位置开始打印
    public void display2(ListNode newHead) {
        //this.head.next != null 会丢失一个数据
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字k
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        //绑定位置的时候一定要先绑定后边
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            //cur.next = null;
            cur.next = node;
        }
    }

    public ListNode findindex(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findindex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现的关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("单链表为空，不能删除！");
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println("未找到该节点");
    }

    //删除所有值为key的节点
    public ListNode removeAllKey(int key) {
        if (this.head == null) return null;

        ListNode prev = this.head;
        ListNode cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后处理头
        if (this.head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }

    //清空链表
    public void clear() {
        //this.head = null;
        while (this.head != null) {
            ListNode curNext = this.head.next;
            this.head.next = null;
            this.head = curNext;
        }
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        //判空
        if (this.head == null) {
            return null;
        }
        ListNode prev = null;
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }

    //返回中间节点 - 快慢指针
    public ListNode middleNode(ListNode head) {
        if (this.head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            if (fast == null) {
                return slow;
            }
            slow = slow.next;
        }

        return slow;
    }

    //倒数第k个结点
    public ListNode FindKthToTail(ListNode head, int k) {
        if (k < 0 || this.head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while (fast.next != null) {
            if (k - 1 != 0) {
                fast = fast.next;
                k--;
            } else {
                fast = fast.next;
                slow = slow.next;
            }
        }
        if (k - 1 != 0) {
            return null;
        }
        return slow;
    }

    //合并有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            } else {
                tmp.next = list2;
                list2 = list2.next;
                tmp = tmp.next;
            }
        }
        if (list1 != null) {
            tmp.next = list1;
        } else {
            tmp.next = list2;
        }
        return newHead.next;
    }

    //链表分割
    public ListNode partition(ListNode pHead, int x) {
        if (this.head == null) {
            return null;
        }
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //第一个段为空
        if (bs == null) {
            return as;
        }
        //预防最后一个节点非空
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    //去掉重复的节点
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode cur = this.head;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (cur != null){
            if (cur.next != null && cur.val == cur.next.val){
                while (cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            } else{
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;
        return newHead.next;
    }

    //回文链表
    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode fast = A;
        ListNode slow = A;
        ListNode cur = A;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (fast != null && fast.next != null){
            cur = cur.next;
            fast = fast.next.next;
            slow.next = tmp;
            tmp = slow;
            slow = cur;
        }
        while (slow != null){
            if (slow.val != tmp.val){
                return false;
            }
            slow = slow.next;
            tmp = tmp.next;
        }
        return true;
    }

    //环形链表I
    public boolean hasCycle(ListNode head) {
        if (head == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }

    //环形链表II
    public ListNode detectCycle(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                break;
            }
        }
        if (fast == null || fast.next == null){
            return null;
        }
        ListNode cur = head;
        while (cur != fast){
            fast = fast.next;
            cur = cur.next;
        }
        return cur;
    }

    //寻找公共节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null){
            return null;
        }
        ListNode last = headB;
        while (last.next != null){
            last = last.next;
        }
        last.next = headB;

        ListNode fast = headA;
        ListNode slow = headA;

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                fast = headA;
                while (fast != slow){
                    fast = fast.next;
                    slow = slow.next;
                }
                last.next = null;
                return slow;
            }
        }
        last.next = null;
        return null;
    }

    //
}

